package model;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.sound.sampled.AudioFormat;

import network.TCPService;
import network.UDPReceiveAndRead;
import network.UDPRecordAndSend;

public class Call {

	private EnumCallStatus status;
	private String lastName; //prenom
	private String firstName; //nom
	private InetAddress ip;
	private int localUdpPort;
	private int distUdpPort;
	private Boolean muteSpeaker =false;
	private Boolean muteMicrophone = false;
	private Boolean clickedOnGui = false;
	private UDPRecordAndSend udpRecordAndSend;
	private UDPReceiveAndRead udpReceiveAndRead;
	private TCPService tcpService;
	private AudioFormat audioFormat;
	private List<ChatMessage> allChatMessage = new ArrayList<ChatMessage>();
	private float voiceActivationLevel = 0;


public Call(){
}
	
public void initCallForSendRequest(InetAddress address, int port, TCPService tcpService, int distPort, AudioFormat audioToBeUse){
	this.status=EnumCallStatus.ACTIV_REQUEST_SEND;
	this.lastName="not determined";
	this.firstName="not determined";
	this.ip=address;
	this.localUdpPort=port;
	this.tcpService=tcpService;
	this.localUdpPort=distPort;
	this.audioFormat=audioToBeUse;
}

public void initCallWithCallRequestSignal(Signal signal, InetAddress ip, TCPService tcpService){
	this.status=EnumCallStatus.REQUEST_RECEIVE;
	this.distUdpPort = signal.getUdpListening();
	this.audioFormat = signal.getAudioWrapper().getAudioFormat();
	this.firstName=signal.getFirstName();
	this.lastName=signal.getLastName();
	this.ip=ip;
	this.tcpService=tcpService;
	
}

//constructeur de test, a virer apres
public Call(InetAddress address, int port, String lastName, String firstName){
	this.ip=address;
	this.localUdpPort=port;
	this.firstName=firstName;
	this.lastName=lastName;
	this.status=EnumCallStatus.REQUEST_RECEIVE;
}


public String getLastName() {
	return lastName;
}

public String getFirstName() {
	return firstName;
}

public void setLastName(String lastName) {
	this.lastName = lastName;
}

public void setFirstName(String firstName) {
	this.firstName = firstName;
}

public EnumCallStatus getStatus() {
	return status;
}

public void setStatus(EnumCallStatus status) {
	this.status = status;
}

public InetAddress getIp() {
	return ip;
}

public void setIp(InetAddress ip) {
	this.ip = ip;
}

public int getLocalUdpPort() {
	return localUdpPort;
}

public boolean isClickedOnGui(){
	return clickedOnGui;
}

public void setClickedOnGui(boolean arg){
	this.clickedOnGui = arg;
}

public TCPService getTcpService() {
	return tcpService;
}

public void setTcpService(TCPService tcpService) {
	this.tcpService = tcpService;
}


public String getMessageFromCall(){
	String result = new String();
	
	//filtrer les message pour obtenir seulement ceux qui concerne le call
	for (ChatMessage chatMsg : allChatMessage) {
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("HH:mm:ss");
			String time = sdf.format(chatMsg.getDate());
			result += "("+time+") "+chatMsg.getAuthor()+" : "+chatMsg.getContent()+" \n";
			}
	
	
	
	return result;
	
}

public List<ChatMessage> getAllChatMessage() {
	return allChatMessage;
}

public int getDistUdpPort() {
	return distUdpPort;
}

public void setDistUdpPort(int distUdpPort) {
	this.distUdpPort = distUdpPort;
}

public void setLocalUdpPort(int localUdpPort) {
	this.localUdpPort = localUdpPort;
}

public void initCanaux() {
	udpReceiveAndRead = new UDPReceiveAndRead(this.localUdpPort, audioFormat,this);
	udpRecordAndSend = new UDPRecordAndSend(this.ip, distUdpPort, audioFormat,this);

	udpReceiveAndRead.start();
	udpRecordAndSend.start();
	
	
	
}

public Boolean getMuteSpeaker() {
	return muteSpeaker;
}

public void switchMuteSpeaker(){
	if(this.muteSpeaker== true) this.muteSpeaker = false;
	else this.muteSpeaker = true;
}

public Boolean getMuteMicrophone(){
	return muteMicrophone;
}

public void switchMuteMicrophone(){
	if(this.muteMicrophone== true) this.muteMicrophone = false;
	else this.muteMicrophone = true;
}

public float getVoiceActivationLevel() {
	return voiceActivationLevel;
}

//public float getCurrentLevel() {
//	return this.udpRecordAndSend.getLevel();
//}

public void setVoiceActivationLevel(int voiceActivationLevel) {
	this.voiceActivationLevel = voiceActivationLevel;
}

public UDPRecordAndSend getUdpRecordAndSend() {
	return udpRecordAndSend;
}

public UDPReceiveAndRead getUdpReceiveAndRead() {
	return udpReceiveAndRead;
}

public AudioFormat getAudioFormat() {
	return audioFormat;
}





}